package cn.jdy.common.ctrl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.util.SMSUtil;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.MemberService;


@Controller
@RequestMapping("/ajaxUtil")
public class AjaxUtilCtrl extends BaseCtrl {

	@Autowired
	private MemberService memberService;//会员分类业务处理类
	
	/**
   * 注册 发送手机验证码
   */
  @RequestMapping("/registeredPhoneCode")
  @ResponseBody
  public Map<String, Object> registeredPhoneCode(String phonecode, String vcode) {
    Map<String, Object> jsonMap = new HashMap<String, Object>();
    try {
      String code = (String) session.getAttribute("verifyCode");
      if(null == phonecode || "".equals(phonecode) || null == vcode || "".equals(vcode) || code == null || "".equals(code) || !code.equals(vcode)){
          jsonMap.put("success", "500");
          jsonMap.put("reMsg", "验证码不对");
      }else{
        if(code.equals(vcode)){
          if(memberService.findcheckUsermobile(phonecode) > 0) {
            jsonMap.put("success", "500");
            jsonMap.put("reMsg", "该手机号已被注册");
          }else{
            sendMsg(phonecode);
            jsonMap.put("success", "0");
          }
        }
      }
    } catch(Exception e) {
      jsonMap.put("success", "500");
      BaseLog.e(AjaxUtilCtrl.class, "发送短信验证码异常", e);
    }
    
    return jsonMap;
  }

  /**
   * 找回密码发送手机验证码
   */
  @RequestMapping("/findbackPhoneCode")
  @ResponseBody
  public Map<String, Object> findbackPhoneCode(String phonecode,String vcode, String token) {
    Map<String, Object> jsonMap = new HashMap<String, Object>();
    
    try {
      boolean falg =true;
      String code = (String) session.getAttribute("verifyCode");
      if(null == phonecode || "".equals(phonecode) || null == vcode || "".equals(vcode) || code == null || "".equals(code) || !code.equals(vcode)){
        jsonMap.put("success", "500");
        jsonMap.put("reMsg", "填写错误");
      }else{
        if(code.equals(vcode)){
          if("remenber".equals(token)){
            Member member = memberService.findMemberByUsername(phonecode);
            if(member==null){
              jsonMap.put("success", "500");
              jsonMap.put("reMsg", "您所填写的手机号暂未注册为网站用户");
              falg = false;
            }else if("1".equals(member.getUserType())){
              jsonMap.put("success", "500");
              jsonMap.put("reMsg", "企业用户请联系管理员找回密码");
              falg = false;
            }
          }
          if(falg){
            sendMsg(phonecode);
            jsonMap.put("success", "0");
          }
        }
      }
    } catch(Exception e) {
      jsonMap.put("success", "500");
      BaseLog.e(AjaxUtilCtrl.class, "发送短信验证码异常", e);
    }
    
    return jsonMap;
  }
  
  /**
   * 手机版找回密码发送手机验证码
   */
  @RequestMapping("/wapfindbackPhoneCode")
  @ResponseBody
  public Map<String, Object> wapfindbackPhoneCode(String phonecode) {
    Map<String, Object> jsonMap = new HashMap<String, Object>();
    try {
      boolean falg =true;
      if(null == phonecode || "".equals(phonecode) ){
        jsonMap.put("success", "500");
        jsonMap.put("reMsg", "手机号填写错误");
      }else{
        Member member = memberService.findMemberByUsername(phonecode);
        if(member==null){
          jsonMap.put("success", "500");
          jsonMap.put("reMsg", "您所填写的手机号暂未注册为网站用户");
          falg = false;
        }else if("1".equals(member.getUserType())){
          jsonMap.put("success", "500");
          jsonMap.put("reMsg", "企业用户请联系管理员找回密码");
          falg = false;
        }
        if(falg){
          sendMsg(phonecode);
          jsonMap.put("success", "0");
        }
      }
    } catch(Exception e) {
      jsonMap.put("success", "500");
      BaseLog.e(AjaxUtilCtrl.class, "发送短信验证码异常", e);
    }
    
    return jsonMap;
  }
	/**
	 * 登录后才可发送
	 * 生成手机验证码
	 */
	@RequestMapping("/sendPhoneCode")
	@ResponseBody
	public Map<String, Object> phoneCode(String phonecode, String token) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		try {
			boolean falg =true;
			if("remenber".equals(token)){
				Member member = memberService.findMemberByUsername(phonecode);
				if(member==null){
					jsonMap.put("success", "500");
					jsonMap.put("reMsg", "您所填写的手机号暂未注册为网站用户");
					falg = false;
				}else if("1".equals(member.getUserType())){
					jsonMap.put("success", "500");
					jsonMap.put("reMsg", "企业用户请联系管理员找回密码");
					falg = false;
				}
			}
			if(falg){
			  sendMsg(phonecode);
		    jsonMap.put("success", "0");
			}
		} catch(Exception e) {
			jsonMap.put("success", "500");
			BaseLog.e(AjaxUtilCtrl.class, "发送短信验证码异常", e);
		}
		
		return jsonMap;
	}

	private void sendMsg(String phonecode) throws IOException{
    if(phonecode.matches("^1\\d{10}$")){
      // 生成6位随机数
      String mobileCode = (Math.random() + "").substring(2, 8);// 生成验证码 6位数字验证码
  
      session.setAttribute(GlobalConstant._PHONECODE,mobileCode);
      
      try {
//      String res = SMSUtil.sms10690071(phonecode, mobileCode);
        String res = SMSUtil.yuntongxun(phonecode,mobileCode,"10");
        BaseLog.d(AjaxUtilCtrl.class, res);
      } catch (Exception e) {
        BaseLog.e(AjaxUtilCtrl.class, "短信验证码发送异常", e);
      }
    }
	}
    
  /**
   * @return
   * @title
   * @description: 校验短信验证码
   * @author: wuzhiliang
   * @date: 2014-03-13 16:53
   */
  @RequestMapping("/checkAuthcode")
  @ResponseBody
  public Map<String, Object> checkAuthcode(String param) {
    Map<String, Object> jsonMap = new HashMap<String, Object>();
    if(param!=null && param.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))) {
      jsonMap.put("success", "0");
    } else {
      jsonMap.put("success", "1");
      jsonMap.put("info", "验证码错误");
    }
    return jsonMap;
  }  
}
